<?php
/*
ALTER TABLE  `posts` ADD  `created_at` DATE NOT NULL


*/
class System_Data_Adapter_Pdo_Mysql extends System_Data_Adapter_Pdo
{
    private $_columnTypeMap = array(
        // string
        'char'      => 'char',
        'varchar'   => 'varchar',
        'text'      => 'text',

        // numeric
        'int'       => 'int',
        'tinyint'   => 'tinyint',
        'bigint'    => 'bigint',
        'float'     => 'float',

        // date & time
        'date'      => 'date',
        'time'      => 'time',
        'timestamp' => 'datetime',
        'datetime' => 'datetime'
    );

    public function createTable($table, $fields)
    {
        if(!is_array($fields) || empty($fields)) {
            throw new Exception('Create table fields invalid');
        }

        // array of column definitions
        $columns = array();

        // loop through each column and get its definition
        foreach ($fields as $name => $value) {
            echo $name;
            $columns[] = $this->_buildColumnSql($name, $value);
        }

        // no errors, build a return the CREATE statement
        $cols = implode(",\n    ", $columns);
        $sql = "CREATE TABLE `$table` (\n    $cols\n)";

        // Build sql options
        $sql .= " ENGINE=InnoDB"; // for transactions
        $sql .= " DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci"; // for UTF8
        // echo $sql;
        //return $this->query($sql);
    }

    // select 'user_id', 'username' from users
    private function _buildColumnSql($name, $info)
    {
        // make sure the column name is a valid identifier
        //$this->_checkIdentifier('column', $name);

        // short-form of definition
        if (is_string($info)) {
            $info = array('type' => $info);
        }

        // set default values for these variables
        $tmp = array(
            'type'    => null,
            'size'    => null,
            'scope'   => null,
            'default' => null,
            'require' => null,
            'primary' => false,
            'autoinc' => false,
        );

        $info = array_merge($tmp, $info);
        extract($info); // see array keys, above

        // force values
        $name    = trim(strtolower($name));
        $type    = strtolower(trim($type));
        $size    = (int) $size;
        $scope   = (int) $scope;
        $require = (bool) $require;

        // is it a recognized column type?
        if (! array_key_exists($type, $this->_columnTypeMap)) {
            throw new Exception("Error column type: $name => $type");
        }

        // basic declaration string
        switch ($type) {

            case 'char':
            case 'varchar':
                // does it have a valid size?
                if ($size < 1 || $size > 255) {
                    throw new Exception("Error column size: $name => $size");
                } else {
                    // replace the 'size' placeholder
                    $coldef = $this->_columnTypeMap[$type] . "($size)";
                }
                break;

            case 'int':
            case 'bigint':
                if(empty($size)) {
                    $size = 11;
                }

                if ($size < 1 || $size > 20) {
                    throw new Exception("Error column size: $name => $size");
                } else {
                    // replace the 'size' placeholder
                    $coldef = $this->_columnTypeMap[$type] . "($size)";
                }
                break;

            case 'numeric':

                if ($size < 1 || $size > 255) {
                    throw new Exception("Error column size: $name => $size, $scope");
                }

                if ($scope < 0 || $scope > $size) {
                    throw new Exception("Error column scope: $name => $size, $scope");
                }

                // replace the 'size' and 'scope' placeholders
                $coldef = $this->_columnTypeMap[$type] . "($size,$scope)";

                break;

            default:
                $coldef = $this->_columnTypeMap[$type];
                break;

        }

        // set the "NULL"/"NOT NULL" portion
        $coldef .= ($require) ? ' NOT NULL' : ' NULL';

        // set the default value, if any.
        // use isset() to allow for '0' and '' values.
        if (isset($default)) {
            $coldef .= ' DEFAULT ' . $this->quote($default);
        } else if($type == 'datetime') {
            $coldef .= " DEFAULT '0000-00-00 00:00:00'";
        } else if($type == 'tinyint') {
            $coldef .= " DEFAULT '0'";
        }

        // modify with auto-increment and primary-key portions
        if ($autoinc) {
            $coldef .= " AUTO_INCREMENT";
        }
        if ($primary) {
            $coldef .= " PRIMARY KEY";
        }

        // done
        return "`$name` $coldef";
    }

    public function alterTable($name, $fields)
    {

    }

    public function addColumn($table, $name, array $attributes)
    {

    }

    public function _dropColumn($table, $name)
    {
        $sql = "ALTER TABLE `$table` DROP COLUMN `$name`";
    }

    public function modifyColumn()
    {

    }
}
